package com.pdstudio.apapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.pdstudio.app.Application;
import com.pdstudio.bean.NewStationState;
import com.pdstudio.bean.StationState;
import com.pdstudio.view.ViewHolderStateList;
import com.pdstudio.weather.R;

public class SingleStationStateListAdapter extends BaseAdapter implements
		Application.EventHandler {

	private List<NewStationState> mStates = new ArrayList<NewStationState>();
	private LayoutInflater inflater;
	private Application mApplication;

	public SingleStationStateListAdapter(Context context, List<NewStationState> states) {
		mStates = states;
		inflater = LayoutInflater.from(context);
		Application.mListeners.add(this);
		mApplication = Application.getInstance();
	}

	@Override
	public int getCount() {
		if (mStates == null) {
			return 0;
		} else {
			return (mStates.size());
		}
	}

	@Override
	public Object getItem(int position) {
		if (mStates == null) {
			return null;
		} else {
			return mStates.get(position);
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolderStateList holder;

		try {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.listview_stationstate_item, null);
				holder = new ViewHolderStateList();
				/** 得到各个控件的对象 */
				
				holder.stateStationName = (TextView) convertView
						.findViewById(R.id.newstationstate_name);
				holder.stateFallWater = (TextView) convertView
						.findViewById(R.id.newstationstate_fall);
				holder.maxStateTemp = (TextView) convertView
						.findViewById(R.id.newstationstate_high_temp);
				holder.minStateTemp = (TextView) convertView
						.findViewById(R.id.newstationstate_low_temp);
				convertView.setTag(holder);// 绑定ViewHolder对象
			}

			else {
				holder = (ViewHolderStateList) convertView.getTag();// 取出ViewHolder对象
			}

			/** 设置TextView显示的内容，即我们存放在动态数组中的数据 */

			holder.stateStationName.setText(mStates.get(position).address);
			holder.stateFallWater.setText(mStates.get(position).precipitation);
			holder.maxStateTemp.setText( mStates.get(position).max_temperature);
			holder.minStateTemp.setText( mStates.get(position).min_temperature);
			

		} catch (Exception e) {
			e.printStackTrace();
		}

		return convertView;
	}

	@Override
	public void onCityComplite() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onNetChange() {
		// TODO Auto-generated method stub

	}

}
